/**
 * FileName: UserConsumerController
 * Author:   powersi
 * Date:     2021/1/31 21:40
 * Description: 1
 */
package comzm.demo.sc.consul.consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * Description: 1
 * Author:      powersi
 * Date:        2021/1/31 21:40
 */
@RestController
public class UserConsumerController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping("/getUser")
    public String getUser(String userName){
        ServiceInstance serviceInstance = loadBalancerClient.choose("sc-consul-server");
        System.out.println("服务地址：" + serviceInstance.getUri());
        System.out.println("服务名称：" + serviceInstance.getServiceId());

        Map<String,String> map = new HashMap<>();
        map.put("userName", userName);
        String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/user/getUser?userName="+ userName, String.class, map);
        System.out.println(callServiceResult);
        return callServiceResult;
    }



    public static void main(String[] args) {
//        ((HttpSecurity)((HttpSecurity)((ExpressionUrlAuthorizationConfigurer.AuthorizedUrl)((ExpressionUrlAuthorizationConfigurer.AuthorizedUrl)http.antMatcher("/**").authorizeRequests().antMatchers(this.permits.split(","))).permitAll().anyRequest()).authenticated().and()).headers().frameOptions().disable().and()).addFilterBefore(this.oauth2SsoFilter(), BasicAuthenticationFilter.class).addFilterAfter(new UserContextFilter(), UsernamePasswordAuthenticationFilter.class).addFilterAfter(new RefererFilter(), UserContextFilter.class).addFilterAfter(new ApiAuthorizationFilter(), UserContextFilter.class).csrf().csrfTokenRepository(this.cookieCsrfTokenRepository()).requireCsrfProtectionMatcher(this.csrfSecurityRequestMatcher());

        String requestUrl = "/ivc/api/accountmgree/queryAcctIncLed";
        AntPathMatcher matcher = new AntPathMatcher();
        if (matcher.match("/api/**", requestUrl)) {
            System.out.println("/api/ === " + true);
        } else {
            System.out.println("/*/api/** === " + matcher.match("/*/api/**", requestUrl));
        }
    }
}
